/*
 * sso
 */
package com.opensso.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.opensso.core.base.BaseDAO;
import com.opensso.core.base.BaseSVImpl;
import com.opensso.core.entity.Page;
import com.opensso.core.exceptions.BaseException;
import com.opensso.core.exceptions.DomainPoolException;
import com.opensso.domain.dao.DomainPoolDAO;
import com.opensso.domain.entity.DomainPool;
import com.opensso.domain.entity.DomainPoolState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("domainPoolSV")
@CacheConfig(cacheNames = "domainPool")
@Slf4j
public class DomainPoolSVImpl extends BaseSVImpl<DomainPool, Long> implements DomainPoolSV {

    @Autowired
    private DomainPoolDAO domainPoolDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return domainPoolDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @CachePut(key = "#entity.domain")
    @Override
    public void save(DomainPool entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象DomainPool
     *
     * @param id id* @param code 编码
     * @return DomainPool
     */
    @Cacheable(key = "#code")
    @Override
    public DomainPool load(java.lang.Long id, java.lang.String code) {
        if (id == null && code == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return domainPoolDAO.load(param);
    }

    /**
     * 加载一个对象DomainPool
     *
     * @param domain 域名
     * @return DomainPool
     */
    @Cacheable(key = "#domain")
    @Override
    public DomainPool loadByDomain(String domain) {
        Map<String, Object> param = new HashMap<>();
        param.put("domain", domain);
        param.put("state", DomainPoolState.Enable.name());
        DomainPool domainPool = domainPoolDAO.load(param);
        return domainPool;
    }

    /**
     * 加载一个对象DomainPool
     *
     * @param apikey apikey
     * @return DomainPool
     */
    @Override
    public DomainPool loadByApiKey(String apikey) {
        Map<String, Object> param = new HashMap<>();
        param.put("apiKey", apikey);
        param.put("state", DomainPoolState.Enable.name());
        DomainPool domainPool = domainPoolDAO.load(param);
        return domainPool;
    }

    /**
     * 加载一个对象DomainPool 通过id
     *
     * @param id id
     * @return DomainPool
     */
    @Cacheable(key = "'id_'+#id")
    @Override
    public DomainPool loadById(java.lang.Long id) {
        if (id == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return domainPoolDAO.loadById(id);
    }

    /**
     * 加载一个对象DomainPool 通过code
     *
     * @param code 编码
     * @return DomainPool
     */
    @Cacheable(key = "#code")
    @Override
    public DomainPool loadByCode(java.lang.String code) {
        if (code == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return domainPoolDAO.loadByCode(code);
    }

    /**
     * 加载一个对象DomainPool
     *
     * @param domain 域名
     * @param apiKey apikey
     * @return DomainPool对象
     */
    @Cacheable(key = "#domain")
    @Override
    public DomainPool load(String domain, String apiKey) {
        Map<String, Object> param = new HashMap<>();
        param.put("domain", domain);
        param.put("apiKey", apiKey);
        param.put("state", DomainPoolState.Enable.name());
        return domainPoolDAO.load(param);
    }


    /**
     * 根据主键id,oldStates 共同更新 DomainPool 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @CachePut(key = "'id_'+#id")
    @Override
    public void updateStateById(java.lang.Long id, DomainPoolState newState, DomainPoolState... oldStates) {
        if (id == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        domainPoolDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键code,oldStates 共同更新 DomainPool 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @CachePut(key = "#code")
    @Override
    public void updateStateByCode(java.lang.String code, DomainPoolState newState, DomainPoolState... oldStates) {
        if (code == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        domainPoolDAO.updateStateByCode(code, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 DomainPool 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
    @CachePut(key = "'id_'+#id")
    @Override
    public void updateById(java.lang.Long id, DomainPoolState state) {
        if (id == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        domainPoolDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键code 更新 DomainPool 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @CachePut(key = "#code")
    @Override
    public void updateByCode(java.lang.String code, DomainPoolState state) {
        if (code == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        domainPoolDAO.updateByCode(code, state, new Date());
    }


    /**
     * 删除对象DomainPool
     *
     * @param id   id
     * @param code 编码
     * @return DomainPool
     */
    @CacheEvict(key = "#code")
    @Override
    public void delete(java.lang.Long id, java.lang.String code) {
        if (id == null && code == null) {
            throw new DomainPoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        domainPoolDAO.delete(param);
    }


    /**
     * 查询DomainPool分页
     *
     * @param domainPool 对象
     * @param offset     查询开始行
     * @param limit      查询行数
     * @return List<DomainPool>
     */
    @Override
    public List<DomainPool> list(DomainPool domainPool, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (domainPool != null) {
            map = JSON.parseObject(JSON.toJSONString(domainPool, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return domainPoolDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(DomainPool domainPool) {
        Map<String, Object> map = null;
        if (domainPool != null) {
            map = JSON.parseObject(JSON.toJSONString(domainPool, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return domainPoolDAO.count(map);
    }

    /**
     * 查询DomainPool分页
     *
     * @param id     id* @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<DomainPool>
     */
    @Override
    public List<DomainPool> list(java.lang.Long id, java.lang.String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return domainPoolDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return domainPoolDAO.count(param);
    }

}
